home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / objstrng.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  1KB  |  65 lines

  1.                                  // Chapter 6 - Program 2
  2. #include <iostream.h>
  3.  
  4. class box {
  5.    int length;
  6.    int width;
  7.    char *line_of_text;
  8. public:
  9.    box(char *input_line);             //Constructor
  10.    void set(int new_length, int new_width);
  11.    int get_area(void);
  12. };
  13.  
  14.  
  15. box::box(char *input_line)        //Constructor implementation
  16. {
  17.    length = 8;
  18.    width = 8;
  19.    line_of_text = input_line;
  20. }
  21.  
  22.  
  23. // This method will set a box size to the two input parameters
  24. void box::set(int new_length, int new_width)
  25. {
  26.    length = new_length;
  27.    width = new_width;
  28. }
  29.  
  30.  
  31. // This method will calculate and return the area of a box instance
  32. int box::get_area(void)
  33. {
  34.    cout << line_of_text << "= ";
  35.    return (length * width);
  36. }
  37.  
  38.  
  39. main()
  40. {
  41. box small("small box "),           //Three boxes to work with
  42.     medium("medium box "),
  43.     large("large box ");
  44.  
  45.    small.set(5, 7);
  46.    large.set(15, 20);
  47.    
  48.    cout << "The area of the ";
  49.    cout << small.get_area() << "\n";
  50.    cout << "The area of the ";
  51.    cout << medium.get_area() << "\n";
  52.    cout << "The area of the ";
  53.    cout << large.get_area() << "\n";
  54.    
  55. }
  56.  
  57.  
  58.  
  59.  
  60. // Result of execution
  61. //
  62. // The area of the small box = 35
  63. // The area is the medium box = 64
  64. // The area is the large box = 300
  65.